﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mobile_VTBL.Models;
using Mobile_VTBL.Models.DTO;
using Mobile_VTBL.UI;

namespace Mobile_VTBL.Controllers
{
    public class CategoryController : Controller
    {
        public const int PAGE_SIZE = 20;
        UserContext CTX = new UserContext();
        public ActionResult Index(int id, int? Page)
        {
            ViewBag.SubCategory = getSubCategory(id);
            ViewBag.Id = id.ToString();
            ViewBag.TotalPages = getTotalPage(id);

            if (!Page.HasValue)
                Page = 1;
            ViewBag.Products = getListProduct(id, Page);
            ViewBag.CurrentPage = Page;

            return View();
        }


        private List<SubCategory> getSubCategory(int id)
        {
            List<SubCategory> l = new List<SubCategory>();

            using (var db = new DataContext())
            {
                var Category = db.Categories.SingleOrDefault(x => x.ID == id);

                #region setDefault
                SubCategory aSubCategory = new SubCategory();
                aSubCategory.ID = Category.ParentID == 0 ? id : Category.ParentID;
                aSubCategory.Name = "Xem tất cả";
                l.Add(aSubCategory);
                #endregion

                l.AddRange(db.Categories.Where(x => x.ParentID == (Category.ParentID == 0 ? id : Category.ParentID)).Select(x => new SubCategory { ID = x.ID, Name = x.Name }).ToList());
            }
            return l;
        }

        private List<ProductList> getListProduct(int CategoryID, int? PageCurent)
        {
            if (!PageCurent.HasValue)
                PageCurent = 1;

            List<ProductList> l = new List<ProductList>();
            using (var db = new DataContext())
            {
                int[] lID = { CategoryID };
                Category aCategory = db.Categories.SingleOrDefault(x => x.ID == CategoryID);
                if (aCategory.ParentID == 0)
                {
                    lID = db.Categories.Where(x => x.ParentID == CategoryID).Select(x => x.ID).ToArray();
                }


                l = db.Products.Where(x => lID.Contains(x.CategoryID)).Select(x => new ProductList { ID = x.ID, Images = x.Images, Name = x.Name }).OrderBy(x => x.ID).Skip((PageCurent.Value - 1) * PAGE_SIZE).Take(PAGE_SIZE).ToList().ToList();
            }
            return l;
        }

        private int getTotalPage(int CategoryID)
        {
            using (var db = new DataContext())
            {
                int[] lID = { CategoryID };
                Category aCategory = db.Categories.SingleOrDefault(x => x.ID == CategoryID);
                if (aCategory.ParentID == 0)
                {
                    lID = db.Categories.Where(x => x.ParentID == CategoryID).Select(x => x.ID).ToArray();
                }

                int totalItem = db.Products.Where(x => lID.Contains(x.CategoryID)).Count();

                return totalItem / PAGE_SIZE + (totalItem % 20 == 0 ? 0 : 1);
            }
        }
    }
}
